package com.woniu.bh.common.core.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.web.multipart.MultipartFile;

/**
 * @version: V1.0
 * @author: Xbao
 * @className: UploadController
 * @packageName: com.woniu.project.common.core.upload
 * @description: 文件上传
 * @data: 2020-10-05 23:34
 */

public class UploadHandler {

    public String upload(Integer type, MultipartFile file, String roleId) throws Exception {
        // Endpoint以杭州为例，其它Region请按实际情况填写。
        String endpoint = "https://oss-cn-beijing.aliyuncs.com";
        // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录RAM控制台创建RAM账号。
        String accessKeyId = "LTAI4G35faDxCUe98Qhod69m";
        String accessKeySecret = "pYn7Xsl70SBqTk9w8HSSHiF7QtLt78";
        String bucketName = "woniu-bh-test";
        //根据上传类型确定传入哪个位置
        String objectNamePrefix = "";
        //根据上传类型确定文件名
        String objectName = RandomId.getRandomId()+file.getOriginalFilename();
        if(type.equals(Constants.CUSTOMER_AVATAR)){
            objectNamePrefix = "test/customer-avatar/";
        }
        if(type.equals(Constants.CUSTOMER_IDENTITY_POSITIVE)){
            objectNamePrefix = "test/customer-identity/"+roleId+"/";
        }
        if(type.equals(Constants.CUSTOMER_IDENTITY_NEGATIVE)){
            objectNamePrefix = "test/customer-identity/"+roleId+"/";
        }
        if(type.equals(Constants.BEAUTICIAN_AVATAR)){
            objectNamePrefix = "test/beautician-avatar/";
        }
        if(type.equals(Constants.BEAUTICIAN_IDENTITY_POSITIVE)){
            objectNamePrefix = "test/beautician-identity/"+roleId+"/";
        }
        if(type.equals(Constants.BEAUTICIAN_IDENTITY_NEGATIVE)){
            objectNamePrefix = "test/beautician-identity/"+roleId+"/";
        }
        if(type.equals(Constants.BEAUTICIAN_LICENSE)){
            objectNamePrefix = "test/beautician-license/";
        }
        if(type.equals(Constants.BEAUTYSALON_LICENSE)){
            objectNamePrefix = "test/beautysalon-license/";
        }
        if(type.equals(Constants.BEAUTYSALON_HOUSE_PIC)){
            objectNamePrefix = "test/beautysalon-house-pic/";
        }
        if(type.equals(Constants.BEAUTYSALON_SERVICE)){
            objectNamePrefix = "test/beautysalon-service/";
        }
        if(type.equals(Constants.BEAUTYSALON_ADVERTISEMENT)){
            objectNamePrefix = "test/beautysalon-advertisement/";
        }
        if(type.equals(Constants.MANAGER_AVATAR)){
            objectNamePrefix = "test/manager-avatar/";
        }
        // <yourObjectName>上传文件到OSS时需要指定包含文件后缀在内的完整路径，例如abc/efg/123.jpg。


        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 上传文件流。
//        InputStream inputStream = new FileInputStream(fileLocalPath);
        ossClient.putObject(bucketName,objectNamePrefix+objectName,file.getInputStream());

        // 关闭OSSClient。
        ossClient.shutdown();

        //返回文件网络路径
        return "https://woniu-bh-test.oss-cn-beijing.aliyuncs.com/"+objectNamePrefix+objectName;
    }
}
